package com.benyanyi.image_grid_lib;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.gif.GifDrawable;

/**
 * @author mylove
 * @date 2021/9/29
 * @email ben@yanyi.red
 * @overview
 */
class GlideUtils {
    static void loadImg(ImageView imageView, String imgUrl, int radius) {
        boolean isGif = false;
        if (imgUrl == null || TextUtils.isEmpty(imgUrl)) {
            isGif = false;
        } else {
            int lastIndexOf = imgUrl.lastIndexOf(".gif");
            isGif = lastIndexOf == (imgUrl.length() - 4);
        }
        RoundCornersTransformation transformation = new RoundCornersTransformation(imageView.getContext(), radius, RoundCornersTransformation.CornerType.ALL);
        RequestManager requestManager = Glide.with(imageView.getContext());
        if (isGif) {
            RequestBuilder<GifDrawable> requestBuilder = requestManager.asGif().load(imgUrl)
                    .placeholder(ImageRecycler.mDefaultRes)
                    .fallback(ImageRecycler.mDefaultRes)
                    .error(ImageRecycler.mDefaultRes)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            if (radius > 0) {
                requestBuilder = requestBuilder.transform(transformation);
            }
            requestBuilder.into(imageView);
        } else {
            RequestBuilder<Drawable> requestBuilder = requestManager.load(imgUrl)
                    .placeholder(ImageRecycler.mDefaultRes)
                    .fallback(ImageRecycler.mDefaultRes)
                    .error(ImageRecycler.mDefaultRes)
                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            if (radius > 0) {
                requestBuilder = requestBuilder.transform(transformation);
            }
            requestBuilder.into(imageView);
        }

    }
}
